package spt.jukg.singleton.test.pro_com.waite;

import java.util.concurrent.atomic.AtomicInteger;

public class Resource {
	// 当前资源数量
	private AtomicInteger current_num = new AtomicInteger(0);
	// 资源上线
	private AtomicInteger max_num = new AtomicInteger(100);

	// 资源出库
	public synchronized void comsume() {
		System.out.println("****************消费者统计：剩余：" + current_num + "****************");
		while (current_num.get() == 0) {
			System.out.println("消费者在等待。。。。");
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		current_num.getAndDecrement();
		System.out.println("消费者消费消费完成。。。。");
		this.notify();
		System.out.println("****************消费者统计：剩余：" + current_num + "****************");

	}

	// 资源入库
	public synchronized void produce() {
		System.out.println("****************生产者统计：剩余：" + current_num + "****************");
		while (current_num.get() == max_num.get()) {
			System.out.println("资料饱和，生产者在等待。。。。");
			try {
				wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		current_num.getAndIncrement();
		System.out.println("生产者" + Thread.currentThread().getName() + "生产一次。。。。");
		try {
			this.notify();
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("****************生产者统计：剩余：" + max_num + "****************");

	}

}
